# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=Vulkan-Loader
pkgbase=mingw-w64-vulkan-loader
pkgname=("${MINGW_PACKAGE_PREFIX}-vulkan-loader")
provides=("${MINGW_PACKAGE_PREFIX}-vulkan")
replaces=("${MINGW_PACKAGE_PREFIX}-vulkan")
pkgver=1.2.148
pkgrel=1
pkgdesc='Vulkan Installable Client Driver (ICD) Loader (mingw-w64)'
arch=('any')
url="https://www.khronos.org/vulkan/"
groups=("${MINGW_PACKAGE_PREFIX}-vulkan-devel")
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-vulkan-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/${_realname}/archive/v${pkgver}.tar.gz"
        001-build-fix.patch
        002-proper-def-files-for-32bit.patch
        003-generate-pkgconfig-files.patch)
sha256sums=('8f4dca3d125965dcb4c4f19ff9dddae893b5f071d63cfd9e47658ccae2414843'
            'd0be58fb70d1d0ba66ddfca8c7fe693d1ae5f77e9e2f8341e9a62934e0e94556'
            'd101e9718b5dc974641ab63084b2f55c48633b4f1fc713d386df34960995e73b'
            '5c189b3f76fa53ad12077cd8932423a0cf385f9464cdde60ba711589ccde19ad')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fix.patch
  patch -p1 -i ${srcdir}/002-proper-def-files-for-32bit.patch
  patch -p1 -i ${srcdir}/003-generate-pkgconfig-files.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -D__STDC_FORMAT_MACROS -Wno-unused-function" \
  CPPFLAGS+=" -D__STDC_FORMAT_MACROS" \
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO -D__STDC_FORMAT_MACROS -fpermissive -Wno-unused-function" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    "${extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_TESTS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  if [ "${CARCH}" = "i686" ]; then
    rm -f ${pkgdir}${MINGW_PREFIX}/lib/libvulkan.dll.a
    ${MINGW_PREFIX}/bin/gendef ${pkgdir}${MINGW_PREFIX}/bin/libvulkan-1.dll
    ${MINGW_PREFIX}/bin/dlltool -k -d libvulkan-1.def -l ${pkgdir}${MINGW_PREFIX}/lib/libvulkan.dll.a
  fi

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.TXT ${pkgdir}${MINGW_PREFIX}/share/licenses/vulkan-loader/LICENSE
}
